﻿#ifndef QUICKLISTMODEL_H
#define QUICKLISTMODEL_H
#include <QObject>
#include<QTimer>
#include<QMap>

#include"quicklistmodelbase.hpp"
#include"quicklistitemmodel.h"



class XUANCOMMON_EXPORT QuickListModel:public QuickListModelBase<QuickListItemModel*>
{
    Q_OBJECT
    Q_PROPERTY(bool allChecked READ Allcheck WRITE setAllChecked NOTIFY AllcheckChanged)
    Q_PROPERTY(int visibleCount READ VisibleCount WRITE setVisibleCount NOTIFY VisibleCountChanged )
    Q_PROPERTY(int selectCount READ SelectCount  WRITE setSelectCount NOTIFY  SelectCountChanged)
    Q_PROPERTY(int checkedCount READ  CheckCount WRITE setCheckCount NOTIFY CheckCountChanged)
    Q_PROPERTY(QStringList headerRoles READ HeaderRoles WRITE setHeaderRoles NOTIFY HeaderRolesChanged )
    Q_PROPERTY(Qt::SortOrder sortOrder READ SortOrder WRITE setSortOrder NOTIFY SortOrderChanged)
    Q_PROPERTY(QString sortRoles READ SortRoles WRITE setSortRole NOTIFY SortRolesChanged)

public:
    ///父类alias
    using Super = QuickListModelBase<QuickListItemModel*>;
    ///排序的回调函数
    using SortCallback = std::function<bool (QuickListItemModel*,QuickListItemModel*)>;
    ///可见数量的回调函数
    using VisibleCallback = std::function<bool (QuickListItemModel*)>;


    explicit QuickListModel(QObject *parent =nullptr);
    ~QuickListModel();


    ///begin    //Get Set and Signal
public:
    Qt::SortOrder SortOrder() const{return m_SortOrder;}

    bool Allcheck() const{  return this->m_Allcheck;}

    int VisibleCount() const{return m_VisibleCount;};

    int SelectCount() const{return m_SelectCount;}

    int CheckCount() const{return m_CheckCount;}

    const QStringList &HeaderRoles() const{return m_HeaderRoles;}

    const QString &SortRoles() const{return m_SortRoles;}

    QString    Searchkey(){return this->m_Searchkey;}

signals:
    void CheckCountChanged(int);
    void VisibleCountChanged(int);
    void SelectCountChanged(int);
    void HeaderRolesChanged(const QStringList&);
    void SortOrderChanged(Qt::SortOrder sortOrder);
    void SortRolesChanged(const QString &);
    ///end

signals:
    //选中信号
    void AllcheckChanged(bool allChecked);

    //搜索信号
    void beginSearch();
    void endSearch();

    //更新item状态
    void upDateCalcCount();

    void SignalUpdateCalInfo();

    void SelectedAction();

    void scrollTo(int index);

public:
    ///
    /// \brief data
    /// \param row
    /// \return  返回当前选中行数的data
    Q_INVOKABLE  QVariant data(int row) const;
    ///
    /// \brief check
    /// \param row 行数
    /// \param checked 是否选中
    /// 1 选中 多选 以及全选按钮 都在此处理
    ///
    Q_INVOKABLE void check(int row,bool checked);

    ///
    /// \brief serach
    /// \param searchKey搜索关键字
    ///
    Q_INVOKABLE void search(const QString &searchKey);
    Q_INVOKABLE void searchImmediate(const QString &searchKey);
    Q_INVOKABLE void clearSearChKey(){m_Searchkey.clear();};

    ///
    /// \brief 鼠标选择相关操作
    ///
    Q_INVOKABLE void deSelectAll(); //取消所有选择
    Q_INVOKABLE void selectAll(); //选择所有选择
    Q_INVOKABLE bool isSelected(int row)const;//row行的选中状态
    Q_INVOKABLE void select(int row); //单选
    Q_INVOKABLE void deselect(int row); //取消row行的选中
    Q_INVOKABLE void selectRange(int from ,int to); //范围选中
    Q_INVOKABLE void selectSingle(int row);//单选

    ///
    /// \brief 鼠标和快捷键操作item
    /// \param row
    ///
    Q_INVOKABLE void doPress(int row ,bool shift ,bool ctrl ,bool outRange);
    Q_INVOKABLE void doMove(int row,bool outRange);
    Q_INVOKABLE void  doRelease();


    Q_INVOKABLE void notifyScrollto(int index){ emit scrollTo(index);}


    Q_INVOKABLE virtual void sortByrole();

public slots:
    void onSerach();
    void setAllChecked(bool isAllChecked);
    void setHeaderRoles(const QStringList &headerroles);
    void setSortOrder(Qt::SortOrder sortOrder);
    void setSortRole(const QString &sortRole);
    void setVisibleCount(int newCount);
    void setSelectCount(int newCount);
    void setCheckCount(int count);

public:
    //重新检查当前item的选中数量
    void updateCheckedCount();
    //重新检查当前全选状态
    void updateAllChecked();
    //重新检查当前可见item数量
    void updateVisibleCount();
    //重新检查选中的item
    void updateSelectedCount();
    //隔行显示
    void updateAlternate();
    //重新检查item的所有状态
    void updateCalInfo()override;
    void setVisibleFilter(const VisibleCallback &callback){ this->m_VisibleCallback =callback; }
    void setSortCallbackAscend(const QMap<QString,SortCallback>&callbacksMap){  m_SortCallbackAsc=callbacksMap; }
    void setSortCallbackDescend(const QMap<QString,SortCallback>&callbacksMap){m_SortCallbackDesc =callbacksMap; }



private:
    Qt::SortOrder m_SortOrder = Qt::AscendingOrder; //排序规则
    bool m_Allcheck = false; //选中所有
    int m_VisibleCount = 0; //可见数量
    int m_SelectCount = 0; //选择数量
    int m_CheckCount = 0; //选中数量
    QStringList m_HeaderRoles;
    QString m_SortRoles;

    bool m_IsPressed = false; //是否按下
    int m_LastPressedRow = -1; //上次点击的行数
    QString    m_Searchkey; //保存搜索的关键字
    QTimer  m_SearchTimer;// 搜索定时器



    //以下是回调函数
    VisibleCallback m_VisibleCallback;
    QMap<QString,SortCallback>m_SortCallbackAsc;
    QMap<QString,SortCallback>m_SortCallbackDesc;



};

#endif // QUICKLISTMODEL_H
